<?php
include "../database/database.php";
class CouponBean
{
	public $couponid;
	public $coutitle;
	public $couimage;
	public $couprice;
	public $couponUseNum;
	public $shop;
	public $shopid;
	public $enddate;
	public $startdate;
	public $jianjie;
	public $top_price;
	public $type;
	public $fanwei;
	public $shengyu;
	public $zongshu;
	public $isovertime;
	

	function __construct($couponId,$couTitle,$couImage,$couPrice,$couponUseNum,$shop,$shopid,$enddate,$startdate,$jianjie,$top_price,$type,$fanwei,$shengyu,$zongshu,$isovertime)
	{
		$this->couponid = $couponId;
		$this->coutitle = $couTitle;
		$this->couimage = $couImage;
		$this->couprice = $couPrice;
		$this->couponUseNum = $couponUseNum;
		$this->shop = $shop;
		$this->shopid = $shopid;
		$this->enddate = $enddate;
		$this->startdate = $startdate;
		$this->jianjie = $jianjie;
		$this->top_price = $top_price;
		$this->type = $type;
		$this->fanwei = $fanwei;
		$this->shengyu = $shengyu;
		$this->zongshu = $zongshu;
		$this->isovertime = $isovertime;
	}
}

// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=10;
}
// 分页 end

//优惠券类型，1 新人专享优惠券，2 普通平台优惠券，3 商家优惠券
$sql = "SELECT * FROM  coupon  where shopid=".$_REQUEST[shopid]." and state = 1  order by couponid  desc  limit $de,$size";
$result = $conn->query($sql);
$couPon = array();
if ($result->num_rows > 0 ) {
	while ($row = $result->fetch_assoc()) {
		//剩余
	    $shengyu = $row['number'];
	    $shopid = $row['shopid'];
		//已领取
		$sql1="select count(*) as couponUseNum from mycoupon where  couponid='".$row[couponid]."'";
		$result1=$conn->query($sql1);
		$row1 = $result1->fetch_assoc();
		$couponUseNum = $row1[couponUseNum];
        //总数
        $zongshu =  $shengyu + $couponUseNum;

        $isovertime=0;
         //是否过期
         if($row['state']==2  ||  date('Y-m-d') > date($row[enddate]) ){
              $isovertime=1;
         }
         
		$shop=0;
		$categroyids = $row[categroyids];
		//获取该优惠券的商户名
		if($shopid!=0){
            $sql2="select * from shop where shopid=".$shopid;
			$result2=$conn->query($sql2);
			$row2 = $result2->fetch_assoc();
			if($row2){
			   $shop = $row2[name];
			   $categroyids = $row2[leimu];
			 }
		}

		//获取分类名称
		$cateNames = array();
		$categroyidsArray = explode(",", $categroyids);
		//平台优惠券
		if(in_array("0",$categroyidsArray)){
			$cateNames[] = '任意';
		}else{
            $sql2="select name from categroy where cateid in (".$categroyids.")";
			$result2=$conn->query($sql2);
			while ($row2 = $result2->fetch_assoc()) {
				$cateNames[] = $row2[name];
			}
		}

		$couPon[] = new CouponBean($row['couponid'],$row['title'],$row['srcimages'],$row['price'],$couponUseNum,$shop,$shopid,$row['enddate'],$row['startdate'],$row['jianjie'],$row['top_price'],$row['type'],$cateNames,$shengyu,$zongshu,$isovertime,$row['state']);
	}
}

$conn->close();

echo json_encode(array('code' => 200,'msg' => "查询成功",'data' => $couPon ));
?>